/**
 * Copyright 2009. Felix Dorner.
 * This code is licensed under the Eclipse Public License v1.0
 * http://www.eclipse.org/org/documents/epl-v10.html
 *
 * $Id$
 */
package com.objectcarpentry.kaylee.impl;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import com.objectcarpentry.kaylee.AbstractPlaylist;
import com.objectcarpentry.kaylee.IPlaylistController;
import com.objectcarpentry.kaylee.IPlayer;
import com.objectcarpentry.kaylee.IPlayerEvent;
import com.objectcarpentry.kaylee.IPlayerListener;
import com.objectcarpentry.kaylee.KayleePackage;
import com.objectcarpentry.kaylee.Playable;
import org.eclipse.emf.common.notify.Notification;
import com.objectcarpentry.kaylee.PlayableState;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Abstract Playlist</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.objectcarpentry.kaylee.impl.AbstractPlaylistImpl#getController <em>Controller</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public abstract class AbstractPlaylistImpl<T extends Playable> extends PlayableImpl implements AbstractPlaylist<T> {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public static final String copyright = "Copyright 2009. Felix Dorner.\nThis code is licensed under the Eclipse Public License v1.0\nhttp://www.eclipse.org/org/documents/epl-v10.html";

	/**
	 * The cached value of the '{@link #getController() <em>Controller</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getController()
	 * @generated
	 * @ordered
	 */
	protected IPlaylistController<T> controller;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected AbstractPlaylistImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return KayleePackage.Literals.ABSTRACT_PLAYLIST;
	}

	

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public IPlaylistController<T> getController() {
		return controller;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setController(IPlaylistController<T> newController) {
		IPlaylistController<T> oldController = controller;
		controller = newController;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, KayleePackage.ABSTRACT_PLAYLIST__CONTROLLER, oldController, controller));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public abstract EList<T> getElements();


	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case KayleePackage.ABSTRACT_PLAYLIST__CONTROLLER:
				return getController();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case KayleePackage.ABSTRACT_PLAYLIST__CONTROLLER:
				setController((IPlaylistController<T>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case KayleePackage.ABSTRACT_PLAYLIST__CONTROLLER:
				setController((IPlaylistController<T>)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case KayleePackage.ABSTRACT_PLAYLIST__CONTROLLER:
				return controller != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (controller: ");
		result.append(controller);
		result.append(')');
		return result.toString();
	}

	@Override
	public void playOn(IPlayer p, Object parameters){
		p.stop();
		setController(new AbstractPlaylistController(p, parameters));
		if (getController() != null){
			p.addPlayerListener(controller);
			setState(PlayableState.PLAYING);
		}
	}
	
	@Override
	public void next(){
		if (controller != null){
			controller.next();
		}
	}
	
	@Override
	public void previous(){
		if (controller != null){
			controller.previous();
		}
	}
	
	
 public class AbstractPlaylistController implements IPlaylistController<T> {
		
		private Playable current;
		protected IPlayer player;
		
		public AbstractPlaylistController(IPlayer p, Object parameters){
			
			if (parameters instanceof Playable ){
				current = (Playable) parameters;
			} else {
				current = getElements().get(0);
			}
			
			this.player = p;
			current.playOn(p, null);
		}
		
		public void next(){
			int index = getElements().indexOf(current) + 1;
			if (index < getElements().size()){
				current = getElements().get(index);
				//current.setState(PlayableState.PLAYING);
				current.playOn(player, null);
			} else {
				player.stop();
			}
		}
		
		public void previous(){
			//current.setState(PlayableState.STOPPED);
			int index = Math.max(0, getElements().indexOf(current) - 1);
			if (index < getElements().size()){
				current = getElements().get(index);
				//current.setState(PlayableState.PLAYING);
				current.playOn(player, null);
			} else {
				player.stop();
			}
		}
	
		
		@Override
		public void handleEvent(IPlayerEvent e) {
			switch (e.getType()){
			case EOS:
			case ERROR: 
				next();
				break;
			case STOP: 
				setState(PlayableState.STOPPED);
				player.removePlayerListener(this);
				break;
			case PAUSE: 
				setState(PlayableState.PAUSED);
				break;
			case PLAY: 
				setState(PlayableState.PLAYING);
				break;
			}
		}
	}
 
 	
} //AbstractPlaylistImpl
